package com.fr.web.core.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SmartUpload
{
  protected byte[] m_binArray;
  protected HttpServletRequest m_request;
  protected HttpServletResponse m_response;
  protected ServletContext m_application;
  private int m_totalBytes = 0;
  private int m_currentIndex = 0;
  private int m_startData = 0;
  private int m_endData = 0;
  private String m_boundary = "";
  private long m_totalMaxFileSize = 0L;
  private long m_maxFileSize = 0L;
  private Vector m_deniedFilesList = new Vector();
  private Vector m_allowedFilesList = new Vector();
  private boolean m_denyPhysicalPath = false;
  private String m_contentDisposition = "";
  public static final int SAVE_AUTO = 0;
  public static final int SAVE_VIRTUAL = 1;
  public static final int SAVE_PHYSICAL = 2;
  private SmartFiles m_files = new SmartFiles();
  private SmartRequest m_formRequest = new SmartRequest();
  private String[] FileNames;

  /**
   * @deprecated
   */
  public final void init(ServletConfig paramServletConfig)
    throws ServletException
  {
    this.m_application = paramServletConfig.getServletContext();
  }

  /**
   * @deprecated
   */
  public void service(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    this.m_request = paramHttpServletRequest;
    this.m_response = paramHttpServletResponse;
  }

  public final void initialize(ServletConfig paramServletConfig, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException
  {
    this.m_application = paramServletConfig.getServletContext();
    this.m_request = paramHttpServletRequest;
    this.m_response = paramHttpServletResponse;
  }

  public final void initialize(ServletContext paramServletContext, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException
  {
    this.m_application = paramServletContext;
    this.m_request = paramHttpServletRequest;
    this.m_response = paramHttpServletResponse;
  }

  public void upload()
    throws ServletException, IOException, SmartUploadException
  {
    int i = 0;
    int j = 0;
    long l = 0L;
    String str1 = "";
    String str2 = "";
    String str3 = "";
    String str4 = "";
    String str5 = "";
    String str6 = "";
    String str7 = "";
    this.m_totalBytes = this.m_request.getContentLength();
    this.m_binArray = new byte[this.m_totalBytes];
    while (i < this.m_totalBytes)
    {
      int k;
      try
      {
        this.m_request.getInputStream();
        k = this.m_request.getInputStream().read(this.m_binArray, i, this.m_totalBytes - i);
      }
      catch (Exception localException)
      {
        throw new SmartUploadException("Unable to upload.");
      }
      i += k;
    }
    while ((j == 0) && (this.m_currentIndex < this.m_totalBytes))
    {
      if (this.m_binArray[this.m_currentIndex] == 13)
        j = 1;
      else
        this.m_boundary += (char)this.m_binArray[this.m_currentIndex];
      this.m_currentIndex += 1;
    }
    if (this.m_currentIndex == 1)
      return;
    this.m_currentIndex += 1;
    while (this.m_currentIndex < this.m_totalBytes)
    {
      Object localObject;
      String str8 = getDataHeader();
      this.m_currentIndex += 2;
      int i1 = (str8.indexOf("filename") > 0) ? 1 : 0;
      String str9 = getDataFieldValue(str8, "name");
      if (i1 != 0)
      {
        str3 = getDataFieldValue(str8, "filename");
        str1 = getFileName(str3);
        str2 = getFileExt(str1);
        str4 = getContentType(str8);
        str5 = getContentDisp(str8);
        str6 = getTypeMIME(str4);
        str7 = getSubTypeMIME(str4);
      }
      getDataSection();
      if ((i1 != 0) && (str1.length() > 0))
      {
        if (this.m_deniedFilesList.contains(str2))
          throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
        if ((!(this.m_allowedFilesList.isEmpty())) && (!(this.m_allowedFilesList.contains(str2))))
          throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
        if ((this.m_maxFileSize > 0L) && (this.m_endData - this.m_startData + 1 > this.m_maxFileSize))
          throw new SecurityException("Size exceeded for this file : " + str1 + " (1105).");
        l += this.m_endData - this.m_startData + 1;
        if ((this.m_totalMaxFileSize > 0L) && (l > this.m_totalMaxFileSize))
          throw new SecurityException("Total File Size exceeded (1110).");
      }
      if (i1 != 0)
      {
        localObject = new SmartFile();
        ((SmartFile)localObject).setParent(this);
        ((SmartFile)localObject).setFieldName(str9);
        ((SmartFile)localObject).setFileName(str1);
        ((SmartFile)localObject).setFileExt(str2);
        ((SmartFile)localObject).setFilePathName(str3);
        ((SmartFile)localObject).setIsMissing(str3.length() == 0);
        ((SmartFile)localObject).setContentType(str4);
        ((SmartFile)localObject).setContentDisp(str5);
        ((SmartFile)localObject).setTypeMIME(str6);
        ((SmartFile)localObject).setSubTypeMIME(str7);
        if (str4.indexOf("application/x-macbinary") > 0)
          this.m_startData += 128;
        ((SmartFile)localObject).setSize(this.m_endData - this.m_startData + 1);
        ((SmartFile)localObject).setStartData(this.m_startData);
        ((SmartFile)localObject).setEndData(this.m_endData);
        this.m_files.addFile((SmartFile)localObject);
      }
      else
      {
        localObject = new String(this.m_binArray, this.m_startData, this.m_endData - this.m_startData + 1);
        this.m_formRequest.putParameter(str9, (String)localObject);
      }
      if ((char)this.m_binArray[(this.m_currentIndex + 1)] == '-')
        return;
      this.m_currentIndex += 2;
    }
  }

  public int save(String paramString)
    throws ServletException, IOException, SmartUploadException
  {
    return save(paramString, 0);
  }

  public int save(String paramString, int paramInt)
    throws ServletException, IOException, SmartUploadException
  {
    int i = 0;
    if (paramString == null)
      paramString = this.m_application.getRealPath("/");
    if (paramString.indexOf("/") != -1)
      if (paramString.charAt(paramString.length() - 1) != '/')
        paramString = paramString + "/";
    else if (paramString.charAt(paramString.length() - 1) != '\\')
      paramString = paramString + "\\";
    this.FileNames = new String[this.m_files.getCount()];
    for (int j = 0; j < this.m_files.getCount(); ++j)
      if (!(this.m_files.getFile(j).isMissing()))
      {
        this.m_files.getFile(j).saveAs(paramString + this.m_files.getFile(j).getFileName(), paramInt);
        this.FileNames[i] = paramString + this.m_files.getFile(j).getFileName();
        ++i;
      }
    return i;
  }

  public String[] getFileNames()
  {
    String[] arrayOfString = new String[this.FileNames.length];
    System.arraycopy(this.FileNames, 0, arrayOfString, 0, this.FileNames.length);
    return arrayOfString;
  }

  public int getSize()
  {
    return this.m_totalBytes;
  }

  public byte getBinaryData(int paramInt)
  {
    int i;
    try
    {
      i = this.m_binArray[paramInt];
    }
    catch (Exception localException)
    {
      throw new ArrayIndexOutOfBoundsException("Index out of range (1005).");
    }
    return i;
  }

  public SmartFiles getFiles()
  {
    return this.m_files;
  }

  public SmartRequest getRequest()
  {
    return this.m_formRequest;
  }

  public void downloadFile(String paramString)
    throws ServletException, IOException, SmartUploadException
  {
    downloadFile(paramString, null, null);
  }

  public void downloadFile(String paramString1, String paramString2)
    throws ServletException, IOException, SmartUploadException, SmartUploadException
  {
    downloadFile(paramString1, paramString2, null);
  }

  public void downloadFile(String paramString1, String paramString2, String paramString3)
    throws ServletException, IOException, SmartUploadException
  {
    downloadFile(paramString1, paramString2, paramString3, 65000);
  }

  public void downloadFile(String paramString1, String paramString2, String paramString3, int paramInt)
    throws ServletException, IOException, SmartUploadException
  {
    if (paramString1 == null)
      throw new IllegalArgumentException("File '" + paramString1 + "' not found (1040).");
    if (paramString1.equals(""))
      throw new IllegalArgumentException("File '" + paramString1 + "' not found (1040).");
    if ((!(isVirtual(paramString1))) && (this.m_denyPhysicalPath))
      throw new SecurityException("Physical path is denied (1035).");
    if (isVirtual(paramString1))
      paramString1 = this.m_application.getRealPath(paramString1);
    File localFile = new File(paramString1);
    FileInputStream localFileInputStream = new FileInputStream(localFile);
    long l = localFile.length();
    int i = 0;
    byte[] arrayOfByte = new byte[paramInt];
    if (paramString2 == null)
      this.m_response.setContentType("application/x-msdownload");
    else if (paramString2.length() == 0)
      this.m_response.setContentType("application/x-msdownload");
    else
      this.m_response.setContentType(paramString2);
    this.m_response.setContentLength((int)l);
    this.m_contentDisposition = ((this.m_contentDisposition != null) ? this.m_contentDisposition : "attachment;");
    if (paramString3 == null)
      this.m_response.setHeader("Content-Disposition", this.m_contentDisposition + " filename=" + getFileName(paramString1));
    else if (paramString3.length() == 0)
      this.m_response.setHeader("Content-Disposition", this.m_contentDisposition);
    else
      this.m_response.setHeader("Content-Disposition", this.m_contentDisposition + " filename=" + paramString3);
    while (i < l)
    {
      int j = localFileInputStream.read(arrayOfByte, 0, paramInt);
      i += j;
      this.m_response.getOutputStream().write(arrayOfByte, 0, j);
    }
    localFileInputStream.close();
  }

  public void downloadField(ResultSet paramResultSet, String paramString1, String paramString2, String paramString3)
    throws ServletException, IOException, SQLException
  {
    if (paramResultSet == null)
      throw new IllegalArgumentException("The RecordSet cannot be null (1045).");
    if (paramString1 == null)
      throw new IllegalArgumentException("The columnName cannot be null (1050).");
    if (paramString1.length() == 0)
      throw new IllegalArgumentException("The columnName cannot be empty (1055).");
    byte[] arrayOfByte = paramResultSet.getBytes(paramString1);
    if (paramString2 == null)
      this.m_response.setContentType("application/x-msdownload");
    else if (paramString2.length() == 0)
      this.m_response.setContentType("application/x-msdownload");
    else
      this.m_response.setContentType(paramString2);
    this.m_response.setContentLength(arrayOfByte.length);
    if (paramString3 == null)
      this.m_response.setHeader("Content-Disposition", "attachment;");
    else if (paramString3.length() == 0)
      this.m_response.setHeader("Content-Disposition", "attachment;");
    else
      this.m_response.setHeader("Content-Disposition", "attachment; filename=" + paramString3);
    this.m_response.getOutputStream().write(arrayOfByte, 0, arrayOfByte.length);
  }

  public void fieldToFile(ResultSet paramResultSet, String paramString1, String paramString2)
    throws ServletException, IOException, SmartUploadException, SQLException
  {
    try
    {
      if (this.m_application.getRealPath(paramString2) != null)
        paramString2 = this.m_application.getRealPath(paramString2);
      InputStream localInputStream = paramResultSet.getBinaryStream(paramString1);
      FileOutputStream localFileOutputStream = new FileOutputStream(paramString2);
      while ((i = localInputStream.read()) != -1)
      {
        int i;
        localFileOutputStream.write(i);
      }
      localFileOutputStream.close();
    }
    catch (Exception localException)
    {
      throw new SmartUploadException("Unable to save file from the DataBase (1020).");
    }
  }

  private String getDataFieldValue(String paramString1, String paramString2)
  {
    String str1 = "";
    String str2 = "";
    int i = 0;
    str1 = paramString2 + "=" + '"';
    i = paramString1.indexOf(str1);
    if (i > 0)
    {
      int j = i + str1.length();
      int k = j;
      str1 = "\"";
      int l = paramString1.indexOf(str1, j);
      if ((k > 0) && (l > 0))
        str2 = paramString1.substring(k, l);
    }
    return str2;
  }

  private String getFileExt(String paramString)
  {
    int i = 0;
    int j = 0;
    if (paramString == null)
      return null;
    i = paramString.lastIndexOf(46) + 1;
    j = paramString.length();
    String str = paramString.substring(i, j);
    if (paramString.lastIndexOf(46) > 0)
      return str;
    return "";
  }

  private String getContentType(String paramString)
  {
    String str1 = "";
    String str2 = "";
    int i = 0;
    str1 = "Content-Type:";
    i = paramString.indexOf(str1) + str1.length();
    if (i != -1)
    {
      int j = paramString.length();
      str2 = paramString.substring(i, j);
    }
    return str2;
  }

  private String getTypeMIME(String paramString)
  {
    int i = 0;
    i = paramString.indexOf("/");
    if (i != -1)
      return paramString.substring(1, i);
    return paramString;
  }

  private String getSubTypeMIME(String paramString)
  {
    int i = 0;
    i = paramString.indexOf("/") + 1;
    if (i != -1)
    {
      int j = paramString.length();
      return paramString.substring(i, j);
    }
    return paramString;
  }

  private String getContentDisp(String paramString)
  {
    String str = "";
    int i = 0;
    int j = 0;
    i = paramString.indexOf(":") + 1;
    j = paramString.indexOf(";");
    str = paramString.substring(i, j);
    return str;
  }

  private void getDataSection()
  {
    int i = this.m_currentIndex;
    int j = 0;
    int k = this.m_boundary.length();
    this.m_startData = this.m_currentIndex;
    this.m_endData = 0;
    while (true)
    {
      while (true)
      {
        if (i >= this.m_totalBytes)
          break label92;
        if (this.m_binArray[i] != (byte)this.m_boundary.charAt(j))
          break;
        if (j == k - 1)
        {
          this.m_endData = (i - k + 1 - 3);
          break label92:
        }
        ++i;
        ++j;
      }
      ++i;
      j = 0;
    }
    label92: this.m_currentIndex = (this.m_endData + k + 3);
  }

  private String getDataHeader()
  {
    int i = this.m_currentIndex;
    int j = 0;
    int k = 0;
    while (true)
    {
      while (true)
      {
        if (k != 0)
          break label78;
        if ((this.m_binArray[this.m_currentIndex] != 13) || (this.m_binArray[(this.m_currentIndex + 2)] != 13))
          break;
        k = 1;
        j = this.m_currentIndex - 1;
        this.m_currentIndex += 2;
      }
      this.m_currentIndex += 1;
    }
    label78: String str = new String(this.m_binArray, i, j - i + 1);
    return str;
  }

  private String getFileName(String paramString)
  {
    int i = 0;
    i = paramString.lastIndexOf(47);
    if (i != -1)
      return paramString.substring(i + 1, paramString.length());
    i = paramString.lastIndexOf(92);
    if (i != -1)
      return paramString.substring(i + 1, paramString.length());
    return paramString;
  }

  public void setDeniedFilesList(String paramString)
    throws ServletException, IOException, SQLException
  {
    if (paramString != null)
    {
      String str = "";
      for (int i = 0; i < paramString.length(); ++i)
        if (paramString.charAt(i) == ',')
        {
          if (!(this.m_deniedFilesList.contains(str)))
            this.m_deniedFilesList.addElement(str);
          str = "";
        }
        else
        {
          str = str + paramString.charAt(i);
        }
      if (!(str.equals("")))
        this.m_deniedFilesList.addElement(str);
    }
    else
    {
      this.m_deniedFilesList = null;
    }
  }

  public void setAllowedFilesList(String paramString)
  {
    if (paramString != null)
    {
      String str = "";
      for (int i = 0; i < paramString.length(); ++i)
        if (paramString.charAt(i) == ',')
        {
          if (!(this.m_allowedFilesList.contains(str)))
            this.m_allowedFilesList.addElement(str);
          str = "";
        }
        else
        {
          str = str + paramString.charAt(i);
        }
      if (!(str.equals("")))
        this.m_allowedFilesList.addElement(str);
    }
    else
    {
      this.m_allowedFilesList = null;
    }
  }

  public void setDenyPhysicalPath(boolean paramBoolean)
  {
    this.m_denyPhysicalPath = paramBoolean;
  }

  public void setForcePhysicalPath(boolean paramBoolean)
  {
  }

  public void setContentDisposition(String paramString)
  {
    this.m_contentDisposition = paramString;
  }

  public void setTotalMaxFileSize(long paramLong)
  {
    this.m_totalMaxFileSize = paramLong;
  }

  public void setMaxFileSize(long paramLong)
  {
    this.m_maxFileSize = paramLong;
  }

  protected String getPhysicalPath(String paramString, int paramInt)
    throws IOException
  {
    String str1 = "";
    String str2 = "";
    String str3 = "";
    int i = 0;
    str3 = System.getProperty("file.separator");
    if (paramString == null)
      throw new IllegalArgumentException("There is no specified destination file (1140).");
    if (paramString.equals(""))
      throw new IllegalArgumentException("There is no specified destination file (1140).");
    if (paramString.lastIndexOf("\\") >= 0)
    {
      str1 = paramString.substring(0, paramString.lastIndexOf("\\"));
      str2 = paramString.substring(paramString.lastIndexOf("\\") + 1);
    }
    if (paramString.lastIndexOf("/") >= 0)
    {
      str1 = paramString.substring(0, paramString.lastIndexOf("/"));
      str2 = paramString.substring(paramString.lastIndexOf("/") + 1);
    }
    str1 = (str1.length() != 0) ? str1 : "/";
    File localFile = new File(str1);
    if (localFile.exists())
      i = 1;
    if (paramInt == 0)
    {
      if (isVirtual(str1))
      {
        str1 = this.m_application.getRealPath(str1);
        if (str1.endsWith(str3))
          str1 = str1 + str2;
        else
          str1 = str1 + str3 + str2;
        return str1;
      }
      if (i != 0)
      {
        if (this.m_denyPhysicalPath)
          throw new IllegalArgumentException("Physical path is denied (1125).");
        return paramString;
      }
      throw new IllegalArgumentException("This path does not exist (1135).");
    }
    if (paramInt == 1)
    {
      if (isVirtual(str1))
      {
        str1 = this.m_application.getRealPath(str1);
        if (str1.endsWith(str3))
          str1 = str1 + str2;
        else
          str1 = str1 + str3 + str2;
        return str1;
      }
      if (i != 0)
        throw new IllegalArgumentException("The path is not a virtual path.");
      throw new IllegalArgumentException("This path does not exist (1135).");
    }
    if (paramInt == 2)
    {
      if (i != 0)
      {
        if (this.m_denyPhysicalPath)
          throw new IllegalArgumentException("Physical path is denied (1125).");
        return paramString;
      }
      if (isVirtual(str1))
        throw new IllegalArgumentException("The path is not a physical path.");
      throw new IllegalArgumentException("This path does not exist (1135).");
    }
    return null;
  }

  public void uploadInFile(String paramString)
    throws IOException, SmartUploadException
  {
    int i = 0;
    int j = 0;
    if (paramString == null)
      throw new IllegalArgumentException("There is no specified destination file (1025).");
    if (paramString.length() == 0)
      throw new IllegalArgumentException("There is no specified destination file (1025).");
    if ((!(isVirtual(paramString))) && (this.m_denyPhysicalPath))
      throw new SecurityException("Physical path is denied (1035).");
    i = this.m_request.getContentLength();
    this.m_binArray = new byte[i];
    while (j < i)
    {
      int k;
      try
      {
        k = this.m_request.getInputStream().read(this.m_binArray, j, i - j);
      }
      catch (Exception localException1)
      {
        throw new SmartUploadException("Unable to upload.");
      }
      j += k;
    }
    if (isVirtual(paramString))
      paramString = this.m_application.getRealPath(paramString);
    try
    {
      File localFile = new File(paramString);
      FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
      localFileOutputStream.write(this.m_binArray);
      localFileOutputStream.close();
    }
    catch (Exception localException2)
    {
      throw new SmartUploadException("The Form cannot be saved in the specified file (1030).");
    }
  }

  private boolean isVirtual(String paramString)
  {
    if (this.m_application.getRealPath(paramString) != null)
    {
      File localFile = new File(this.m_application.getRealPath(paramString));
      return localFile.exists();
    }
    return false;
  }
}